﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using org.in2bits.MyXls;
using org.in2bits.MyXls.ByteUtil;

namespace NJL.WebManage.Orders
{
    public partial class FarmerOrders : BasePage
    {
        PagedDataSource pds;
        NJL.BLL.NJL_BOOK_ORDER bll = new BLL.NJL_BOOK_ORDER();
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        private readonly NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
        private readonly NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        protected NJL.Model.NJL_ATTRACTION modelAttr;
        protected NJL.Model.NJL_COMPANY modelCompany;
        NJL.BLL.NJL_SYS_BASE bllSysBase = new BLL.NJL_SYS_BASE();
        NJL.Model.NJL_SYS_BASE modelSysBase;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
            {
                txtStartTime.Text = "2011-01-01";
                txtEndTime.Text = DateTime.Now.ToShortDateString();
                ViewState["pager"] = "0";
                if (WEBSITE_CODE == "1")
                {
                    DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlarea.DataSource = ds;
                    ddlarea.DataTextField = "AREA_NAME";
                    ddlarea.DataValueField = "AREA_CODE";
                    ddlarea.DataBind();
                }
                else
                {
                    dispArea.Visible = false;
                }
                bind();
            }
        }
        public void bind()
        {
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            ds = bll.GetList(sql());
            pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }
            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        public string sql()
        {
            string sql = "";
            if (WEBSITE_CODE == "1")
            {
                if (ddlarea.SelectedValue == "000000")
                {
                    sql += "NJL_BOOK_ID in (select NJL_BOOK.ID from NJL_BOOK,NJL_COMPANY where NJL_BOOK.TYPE_CODE='2' and NJL_COMPANY.COMPANY_NAME like '%" + txtCompanyName.Text.Trim() + "%' and BOOK_NAME like '%" + txtTicketType.Text.Trim() + "%' and NJL_BOOK.TARGET_ID=NJL_COMPANY.ID) and ORDER_NO like '%" + txtKeywords.Text.Trim() + "%' and PICK_NAME like '%" + txtPickName.Text.Trim() + "%' and PICK_MOBILE like '%" + txtPickTel.Text.Trim() + "%' and (CREATE_DATE_TIME BETWEEN '" + txtStartTime.Text.Trim() + "' and '" + Convert.ToDateTime(txtEndTime.Text.Trim()).AddDays(1).ToShortDateString() + "')";
                }
                else
                {
                    sql += "NJL_BOOK_ID in (select NJL_BOOK.ID from NJL_BOOK,NJL_COMPANY where NJL_BOOK.TYPE_CODE='2' and NJL_COMPANY.COMPANY_NAME like '%" + txtCompanyName.Text.Trim() + "%' and BOOK_NAME like '%" + txtTicketType.Text.Trim() + "%' and NJL_BOOK.TARGET_ID=NJL_COMPANY.ID) and ORDER_NO like '%" + txtKeywords.Text.Trim() + "%' and PICK_NAME like '%" + txtPickName.Text.Trim() + "%' and PICK_MOBILE like '%" + txtPickTel.Text.Trim() + "%' and (CREATE_DATE_TIME BETWEEN '" + txtStartTime.Text.Trim() + "' and '" + Convert.ToDateTime(txtEndTime.Text.Trim()).AddDays(1).ToShortDateString() + "') and NJL_SYS_BASE_ID='" + ddlarea.SelectedValue + "'";
                }
            }
            else
            {
                sql += "NJL_BOOK_ID in (select NJL_BOOK.ID from NJL_BOOK,NJL_COMPANY where NJL_BOOK.TYPE_CODE='2' and NJL_COMPANY.COMPANY_NAME like '%" + txtCompanyName.Text.Trim() + "%' and BOOK_NAME like '%" + txtTicketType.Text.Trim() + "%' and NJL_BOOK.TARGET_ID=NJL_COMPANY.ID) and ORDER_NO like '%" + txtKeywords.Text.Trim() + "%' and PICK_NAME like '%" + txtPickName.Text.Trim() + "%' and PICK_MOBILE like '%" + txtPickTel.Text.Trim() + "%' and (CREATE_DATE_TIME BETWEEN '" + txtStartTime.Text.Trim() + "' and '" + Convert.ToDateTime(txtEndTime.Text.Trim()).AddDays(1).ToShortDateString() + "')  and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'";
            }
            return sql;
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }

        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //if (e.CommandName == "linkbtn")
            //{
            //    string id = e.CommandArgument.ToString();
            //    model = bll.GetModel(id);
            //    if (model.ORDER_STATE == 0)
            //    {
            //        model.ORDER_STATE = 1;
            //    }
            //    if (bll.Update(model))
            //    {
            //        bind();
            //    }
            //    else
            //    {
            //        Royalsoft.Common.MessageBox.Show(this, "审核失败");
            //    }
            //}
        }
        NJL.BLL.NJL_BOOK bllBook = new BLL.NJL_BOOK();
        NJL.Model.NJL_BOOK modelBook;
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
                LinkButton linkbtn4 = e.Item.FindControl("linkbtn4") as LinkButton;
                Literal litOrderState = e.Item.FindControl("litOrderState") as Literal;
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string bookId = drv["NJL_BOOK_ID"].ToString();
                string orderId = drv["ID"].ToString();
                int orderState = Convert.ToInt32(drv["ORDER_STATE"]);
                modelBook = bllBook.GetModel(bookId);
                if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "2")
                {
                    if (orderState == 1)
                    {
                        litOrderState.Text = "预订成功";
                    }
                }
                else if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "1")  //在线支付
                {
                    if (orderState == 1)
                    {
                        litOrderState.Text = "待支付状态";
                    }
                    else if (orderState == 2)
                    {
                        litOrderState.Text = "支付成功";
                    }
                }
                Literal litType = e.Item.FindControl("litType") as Literal;
                litType.Text = modelBook.BOOK_NAME;
                Literal litName = e.Item.FindControl("litName") as Literal;
                if (modelBook.TYPE_CODE == "2")
                {
                    modelCompany = bllCompany.GetModel(modelBook.TARGET_ID);
                    modelSysBase = bllSysBase.GetModel(modelBook.NJL_SYS_BASE_ID);
                    litName.Text = string.Format("<a href=\"http://{0}/shangpu/{1}-index.html\" target=\"_blank\">{2}</a>", modelSysBase.WEBSITE_DOMAIN, modelCompany.ID, modelCompany.COMPANY_NAME);
                }

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        public void ToExcel(System.Web.UI.Control ctl, string FileName)
        {
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }//导入excel 


    }
}